<template>
  <div class="components-container">
    <div class="header-form">
      <div class="layui-inline">
        <label>处理日期从</label>
        <el-date-picker v-date v-model="selectData.ChddateGreaterAndEquals" type="date" placeholder="输入日期"
          @change="btnSearch" />
        到
        <el-date-picker v-date v-model="selectData.ChddateLessAndEquals" type="date" placeholder="输入日期"
          @change="btnSearch" />
      </div>
      <div class="layui-inline">
        <label class="layui-form-label">收费项</label>
        <div class="layui-input-inline">
          <el-select v-model="selectData.CypidEquals" @change="ReductionType($event)" placeholder="请选择">
            <el-option label="全部" :value="null" />
            <el-option v-for="item in chargeProjectTypes" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </div>
      </div>
      <el-button type="primary" @click="btnSearch" :disabled="downloadLoading" :loading="this.$store.state.isloading">搜索
      </el-button>
      <el-button :disabled="downloadLoading || this.$store.state.isloading" @click="btnRepeat">重置</el-button>
    </div>
    <div style="margin-top:10px;">
      <ul class="toolList">
        <li v-if="btnShow(item.BTNVALUE)" v-for="(item, index) in $store.state.btnListCommon"
          @click="operate(item.BTNVALUE)">
          <el-button type="primary" :disabled="downloadLoading || $store.state.isloading" :value="item.BTNSHOWNAME"
            v-text="item.BTNSHOWNAME"></el-button>
        </li>
      </ul>
    </div>
    <dataTable :headList="headList" :dataList="dataList" :isIndex="true" :pageOrder="pageOrder" @pageChange="pageChange"
      @selectRow="selectRow" :hiddenColumn="hiddenColumn" :hiddenPageOrder="false" :height="height"></dataTable>
    <printmodal ref="printPdf"></printmodal>
    <h1 style="text-align:right;margin-right:100px;color:red;font-size:20px;">{{message}}</h1>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";

.el-button + .el-button {
  margin-left: 0;
}

.btn {
  text-align: center;
}
</style>
<script>
import dataTable from "@/components/Table/DataTable";
import printmodal from "@/components/Pdf/Print";
import apiBase from "@/api/apiBase.js";
import { formatDate, timeStringToDate } from "@/utils/date.js";
import { manage } from "@/mixins/managePage.js";
import { PRINT } from "@/components/Pdf/PDF.js";
import sweetModal from "@/components/sweetModal";

export default {
  name: "businessIncome",
  mixins: [manage],
  data() {
    return {
      selectData: {
        ChddateGreaterAndEquals: null,
        ChddateLessAndEquals: null,
        CypidEquals: null,
        ChdstatusEquals:'B'
      },
      downloadLoading: false,
      OutDataExcel: [],
      height: "620",
      chargeProjectTypes: [],
      hiddenColumn: false,
      message: null
    };
  },
  methods: {
    exportExcel() {
      if (this.dataList.length === 0) {
        this.$message.error("无导出数据！");
        return;
      }
      var startTime;
      var endTime;
      if (this.selectData.ChddateGreaterAndEquals) {
        startTime = this.selectData.ChddateGreaterAndEquals.toLocaleDateString();
      } else {
        startTime = "空";
      }
      if (this.selectData.ChddateLessAndEquals) {
        endTime = this.selectData.ChddateLessAndEquals.toLocaleDateString();
      } else {
        endTime = "空";
      }

      this.downloadLoading = true;
      let headername = "业务收费表" + "(" + startTime + "-" + endTime + ")";
      require.ensure([], () => {
        const { export_json_to_excel } = require("@/vendor/Export2Excel");
        export_json_to_excel(
          this.headList,
          this.dataList,
          headername,
          undefined,
          "jmmxb"
        );
        this.downloadLoading = false;
      });
    },
    //功能操作
    operate(order) {
      this.order = order;
      switch (this.order) {
        case "导出":
          this.exportExcel();
          break;
        case '打印':
          this.$refs.printPdf.report(
            '打印业务收入表',
            this.getDataC()
          );
          break;
        default:
      }
    },
    //减免类型
    ReductionType(val) {
      this.getData();
    },
    getColumns() {
      apiBase.DoService(
        {},
        apiBase.getApiAdress().Column.GetCemBussDitRetColumns,
        res => {
          this.headList = res.data.content;
        }
      );
    },
    getData() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.ChddateGreaterAndEquals = formatDate(
        Data.ChddateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.ChddateLessAndEquals = formatDate(
        Data.ChddateLessAndEquals,
        "yyyyMMdd"
      );
      let data = {
        PageIndex: -1,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      apiBase.DoService(
        data,
        apiBase.getApiAdress().Report.QueryCemBussChaByCyp,
        res => {
          this.dataList = res.data.content.OutData;
          this.message = res.data.content.Message;
        }
      );
    },
    getDataC() {
      let Data = JSON.parse(JSON.stringify(this.selectData));
      Data.ChddateGreaterAndEquals = formatDate(
        Data.ChddateGreaterAndEquals,
        "yyyyMMdd"
      );
      Data.ChddateLessAndEquals = formatDate(
        Data.ChddateLessAndEquals,
        "yyyyMMdd"
      );
      let data = {
        PageIndex: -1,
        OrderBy: this.pageOrder.orderType,
        OrderByType: this.pageOrder.orderSort,
        Data: Data
      };
      return data
    },

    btnSearch() {
      this.getData();
      this.$store.state.isloading = true;
    },
    //查询条件重置
    btnRepeat() {
      this.selectData = {
        ChddateGreaterAndEquals: null,
        ChddateLessAndEquals: null,
        CypidEquals: null,
        ChdstatusEquals:'B'
      };
      this.selectData.ChddateGreaterAndEquals = new Date();
      this.selectData.ChddateGreaterAndEquals.setDate(1);
      this.selectData.ChddateLessAndEquals = new Date();
    },
    btnShow(btn) {
      switch (btn) {
        case "导出":
        case "打印":
          return true;
          break;
        default:
          return false;
      }
    },
    getinitData() {
      apiBase.DoService(
        {},
        apiBase.getApiAdress().ChargeProject.QueryType,
        res => {
          let cdhTypeData = res.data.content,
            legendsCdhData = cdhTypeData.map(item => ({
              label: item.CYPNAME,
              value: item.CYPID
            }));
          this.chargeProjectTypes = legendsCdhData;
        }
      );
    }
  },
  mounted() {},
  components: {
    dataTable,
    sweetModal,
    printmodal
  },

  created() {
    this.selectData.ChddateGreaterAndEquals = new Date();
    this.selectData.ChddateGreaterAndEquals.setDate(1);
    this.selectData.ChddateLessAndEquals = new Date();
    this.getColumns();
    this.getinitData();
  },
  activated() {
    this.getData();
  }
};
</script>
